/**
 * 档案资料附件表管理
 */
var Files = function (layui,noBar) {
    this.table = layui.table;
    this.$ = layui.$;
    this.func = layui.func;
    this.tableId = "file";
    var that = this;
    this.noBar = noBar||false;
    /**
     * 初始化表格的列
     */
    this.initColumn = function () {
        if(noBar) {
            return [[
                {field: 'xh', fixed: 'left', width: 80, sort: true, title: '序号'},
                {field: 'mc', width: 120, title: '附件名称'},
                {field: 'daid', hide: true, sort: true, title: '档案id'},
                {field: 'dazlid', hide: true, sort: true, title: '档案资料id'},
                {
                    field: 'wjlj',
                    title: '文件路径',
                    templet: '<div><a href="/static/{{d.wjlj}}" class="layui-table-link" download="{{d.id}}">下载</a></div>'
                },
                {field: 'scsj', title: '上传时间'},
            ]];
        }else {
            return [[
                {field: 'xh', fixed: 'left', width: 80, sort: true, title: '序号'},
                {field: 'mc', width: 120, title: '附件名称'},
                {field: 'daid', hide: true, sort: true, title: '档案id'},
                {field: 'dazlid', hide: true, sort: true, title: '档案资料id'},
                {
                    field: 'wjlj',
                    title: '文件路径',
                    templet: '<div><a href="/static/{{d.wjlj}}" class="layui-table-link" download="{{d.id}}">下载</a></div>'
                },
                {field: 'scsj', title: '上传时间'},
                {fixed: 'right', width: 120, align: 'center', toolbar: '#tableBar', title: '操作'}
            ]];
        }
    };


    /**
     * 弹出添加对话框
     */
    this.openAddDlg = function () {
        if(!(this.parentId && this.parentId !='-1')){
            Feng.error("请选择所属档案资料");
            return;
        }

        this.func.open({
            height: 600,
            width: 600,
            title: '添加档案资料附件',
            content: Feng.ctxPath + '/archiveMaterialsAttach/add?parentId=' + that.parentId,
            tableId:that.tableId
        });
    };


    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    this.openEditDlg = function (data) {
        this.func.open({
            height: 600,
            width: 600,
            title: '编辑档案资料附件',
            content:  Feng.ctxPath + '/archiveMaterialsAttach/edit?id=' + data.id,
            tableId:that.tableId
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    this.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archiveMaterialsAttach/delete", function (data) {
                Feng.success("删除成功!");
                that.reload();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    // 工具条点击事件
    this.table.on('toolbar(' + this.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'add') {
            that.openAddDlg();
        }
    });


    // 工具条点击事件
    this.table.on('tool(' + this.tableId + ')', function (obj) {
        var layEvent = obj.event;
        var data = obj.data;

        if (layEvent === 'edit') {
            that.openEditDlg(data);
        } else if (layEvent === 'delete') {
            that.onDeleteItem(data);
        }
    });


    // 渲染表格
    this.tableResult = this.table.render({
        elem: '#' + this.tableId,
        data: [],
        page: false,
        height: "500",
        toolbar: '#toolbar',
        defaultToolbar: [],
        cellMinWidth: 100,
        cols: this.initColumn(),
        // done:function (res, cur,count) {
        //     if (cur == 1) {
        //         var selector = ".layui-table-view[lay-id='"+this.tableId+"'] .layui-table-body tr[data-index=0] .layui-form-radio";
        //         $(selector).click();
        //     }
        // }
    });


    this.setParentId = function (id) {
        this.parentId = id;
        this.reload();
    }

    this.reload = function () {
        this.table.reload(that.tableId, {
            url: Feng.ctxPath + '/archiveMaterialsAttach/list',
            where: {'dazlid': that.parentId}
        });
    }
};

    
